package ui.fragment.PosControl;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import net.ck.hzq.R;
import net.ck.hzq.TobuyApplication;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import javax.inject.Inject;

import base.LazyFragment;
import butterknife.BindView;
import butterknife.ButterKnife;
import cn.finalteam.loadingviewfinal.OnDefaultRefreshListener;
import cn.finalteam.loadingviewfinal.PtrClassicFrameLayout;
import cn.finalteam.loadingviewfinal.PtrFrameLayout;
import cn.finalteam.loadingviewfinal.RecyclerViewFinal;
import coom.MyType;
import event.CloseActEvent;
import event.NetErrorEvent;
import ui.activity.poscontrol.adapter.SearchPosInfoAdapter;
import ui.activity.poscontrol.beanmodel.MyPosBean;
import ui.activity.poscontrol.beanmodel.PosInfoBean;
import ui.activity.poscontrol.biz.MyPosControlBiz;
import ui.activity.poscontrol.component.DaggerMyPosControlComponent;
import ui.activity.poscontrol.contract.MyPosControlContract;
import ui.activity.poscontrol.module.MyPosControlModule;
import ui.activity.poscontrol.presenter.MyPosControlPresenter;
/**
 * Created by fangjunsong on 2019年3月19日17:03:30.
 * <p>
 * 审核中
 */

public class InAuditFra extends LazyFragment implements MyPosControlContract.View {


    @Inject
    MyPosControlPresenter presenter;
    @Inject
    MyPosControlBiz biz;


    @BindView(R.id.pull_refresh_rv)
    RecyclerViewFinal rv;
    @BindView(R.id.pull_refresh_ptr)
    PtrClassicFrameLayout ptr;
    @BindView(R.id.pos_num_text)//终端数量txt
            TextView pos_num_text;

    @BindView(R.id.search_text)//费率txt
    TextView search_text;
    SearchPosInfoAdapter adp;
    private SharedPreferences sp;

    private boolean isPrepared;// 界面否
    private boolean isALeady = true;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.my_pos_fraitem_layout, container, false);
        ButterKnife.bind(this, view);
        DaggerMyPosControlComponent.builder().myPosControlModule(new MyPosControlModule(this)).build().inject(this);
        presenter.setBiz(biz);
        search_text.setText("费率");
        sp = getActivity().getSharedPreferences("ToBuy", Context.MODE_PRIVATE);
        rv.setLayoutManager(new LinearLayoutManager(getActivity()));
        rv.setItemAnimator(new DefaultItemAnimator());
        rv.addItemDecoration(new DividerItemDecoration(getActivity(),1));
        ptr.setLastUpdateTimeRelateObject(this);
        ptr.disableWhenHorizontalMove(true);

        rv.setOnLoadMoreListener(() -> {
            presenter.getPosInfoList(TobuyApplication.getCustomId(),2,false);
        });

        ptr.setOnRefreshListener(new OnDefaultRefreshListener() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                presenter.getPosInfoList(TobuyApplication.getCustomId(),2,true);
            }
        });
        isPrepared = true;

        return view;
    }

    @Override
    protected void lazyLoad() {
        if (isPrepared && isVisible && isALeady) {
            // TODO 在这里处理逻辑
            ptr.autoRefresh(true);
            isALeady = false;
        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        biz.onDestory();
        ButterKnife.bind(getActivity()).unbind();
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCloseActEvent(CloseActEvent event) {
        if (event.getType() == MyType.CompleteOrder) {
        }
    }


    @Override
    public void upDateUI(MyPosBean infoBean,List<MyPosBean.RecordsBean> bean,boolean refresh) {

    }

    @Override
    public void upDateInfoListUI(PosInfoBean infoBean, List<PosInfoBean.RecordsBean> bean) {
        pos_num_text.setText("终端数量："+infoBean.getTotal()+"台");
        if (adp == null) {
            adp = new SearchPosInfoAdapter(getActivity(),bean,2);
            rv.setAdapter(adp);
        } else {
            adp.notifyDataSetChanged();
        }

    }

    @Override
    public void refreshComplete() {
        ptr.onRefreshComplete();
    }

    @Override
    public void refreshData() {

    }

    @Override
    public void loadMoreComplete() {
        rv.onLoadMoreComplete();
    }

    @Override
    public void hasLoadMore(boolean b) {
        rv.setHasLoadMore(b);
    }


}
